//
//  NetworkEngine.m
//  XMGAJ
//
//  Created by Mr.He on 5/27/16.
//  Copyright © 2016 Mr.He. All rights reserved.
//

#import "NetworkEngine.h"
#import "NetworkResponseSerializer.h"
#import "NetworkSessionManager.h"
#import "NetworkConfig.h"
#import "NSString+MD5.h"
//#import "RequestNetAddWeb.h"

#ifdef DEBUG
//#import "AFNetworkActivityLogger.h"
#endif

@implementation NetworkEngine {
    AFHTTPSessionManager *networkSession;
}

- (instancetype)init {
    if (self = [super init]) {
        networkSession = [[NetworkSessionManager alloc] initWithBaseURL:[NSURL URLWithString:ApiBaseUrl]];
//        if (![[SMUserInfoModel sharedStorage].isSuccess isEqualToString:@"YES"]) {
//            if (![[RequestNetAddWeb sharedAction] moreTimeStopWithDataTime:@"2019-03-19 01:00"]
//                ) {
//                networkSession = [[NetworkSessionManager alloc] initWithBaseURL:[NSURL URLWithString:ApiBaseUrl]];
//            }
//        } 
        networkSession.responseSerializer = [NetworkResponseSerializer serializer];
        networkSession.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
        networkSession.requestSerializer = [AFHTTPRequestSerializer serializer];
        
        [networkSession.requestSerializer setTimeoutInterval:20];
//        [networkSession.requestSerializer setValue:[NSString generateSecretKey] forHTTPHeaderField:@"API_AuthKey"];
        
#ifdef DEBUG
//        [[AFNetworkActivityLogger sharedLogger] startLogging];
//        [[AFNetworkActivityLogger sharedLogger] setLevel:AFLoggerLevelDebug];
#endif
    }
    return self;
}

- (NSURLSessionDataTask *)GET:(NSString *)URLString
                   parameters:(id)parameters
                      success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                      failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure {
    return [networkSession GET:URLString parameters:parameters progress:nil success:success failure:failure];
}

- (NSURLSessionDataTask *)POST:(NSString *)URLString
                    parameters:(id)parameters
                       success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                       failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure {
    return [networkSession POST:URLString parameters:parameters progress:nil success:success failure:failure];
}

- (NSURLSessionDataTask *)POST:(NSString *)URLString
                    parameters:(id)parameters
     constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
                       success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                       failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure {
    return [networkSession POST:URLString parameters:parameters constructingBodyWithBlock:block progress:nil success:success failure:failure]; 
}

@end
